<template>
  <div>
    <van-nav-bar title="登录" class="page-nav-bar">
        <van-icon slot='left' size="18" name="cross" @click="$router.back()"></van-icon>
    </van-nav-bar>
    <!-- 给表单取别名，绑定点击事件，调用方法去执行 -->
    <van-form @submit="onSubmit" ref="loginForm">
      <van-field
        v-model="mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="[
          { required: true },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号' },
        ]"
      >
        <i slot="left-icon" class="toutiao icon-shouji"></i>
      </van-field>
      <van-field
        v-model="code"
        type="text"
        name="code"
        placeholder="请输入验证码"
        :rules="[
          { required: true },
          { pattern: /^\d{6}$/, message: '请输入正确的验证码' },
        ]"
      >
        <i slot="left-icon" class="toutiao icon-yanzhengma"></i>
        <!-- 插入倒计时组件 ，并与v-button形成互斥 -->
        <van-count-down
          slot="right-icon"
          :time="1000 * 60"
          format="ss s"
          v-if="isCountShow"
          @finish="isCountShow = false"
        />
        <van-button
          v-else
          class="send-sms-btn"
          round
          slot="right-icon"
          type="default"
          size="small"
          @click="onSendSms"
          native-type="button"
          >获取验证码</van-button
        >
      </van-field>
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { getSmsCode, login } from '@/api/user'
export default {
  name: 'LoginPage',
  components: {},
  data () {
    return {
      mobile: '',
      code: '',
      isCountShow: false
    }
  },
  created () {

  },
  mounted () {

  },
  methods: {
    //   绑定事件，添加特殊属性
    async onSendSms () {
      // 1.校验手机号
      try {
        // validate  验证表单，支持传入 name 来验证单个或部分表单项
        await this.$refs.loginForm.validate('mobile')
        console.log('校验成功')
        // 2.显示倒计时
        this.isCountShow = true
        // 3.发请求
        await getSmsCode(this.mobile)
      } catch (error) {
        if (error.response && error.response.status === 429) {
          this.$toast.fail('请稍后再试')
        } else {
          this.$toast.fail('手机验证号出错')
        }
      }
    },
    async onSubmit (values) {
      console.log(values)
      this.$toast.loading({
        duration: 9, // 持续展示 toast
        forbidClick: true,
        message: '加载中...'
      })
      try {
        const res = await login(values)
        console.log(res)
        // 11.3 调用方法
        this.$store.commit('setUser', res.data.data)
        this.$toast.success('登录成功')
        this.$router.back()
      } catch (error) {
        if (error.response.status === 400) {
          this.$toast.fail('验证码出错了')
        } else {
          this.$toast.fail('服务器出错了')
        }
      }
    }
  }

}
</script>

<style lang="less" scoped>
.toutiao {
  font-size: 37px;
}
.send-sms-btn {
  width: 160px;
  height: 46px;
  line-height: 46px;
  background-color: #ededed;
  font-size: 22px;
  color: #666;
}
</style>
